<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        stripe
        border
        :where="defaultWhere"
      >
        <template #status="{ row }">
          <el-tag :type="row.statusTag">{{ row.statusText }}</el-tag>
        </template>
        <template #pdf="{ row }">
          <span class="pointer blue" @click="openPdf(row.pdf)">查看</span>
        </template>
        <template #updatePdf="{ row }">
          <file
            botton-text="更新保单"
            botton-size="small"
            botton-type="success"
            @done="onUpladDone($event, row)"
          />
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button
              v-if="row.canRefund == 1"
              type="danger"
              @click="openRefund(row)"
              size="small"
            >
              退保
            </el-button>
            <el-button
              v-if="row.canRefund == 1"
              type="warning"
              @click="openRefundPlan(row)"
              size="small"
            >
              退保计划
            </el-button>
            <el-button
              v-if="row.refund_pdf != '' && row.statusText == '已退保'"
              type="primary"
              @click="openPdf(row.refund_pdf)"
              size="small"
            >
              退保批单
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
      <!-- 退保 -->
      <Refund v-model="showRefund" :data="current" @done="reload" />
      <!-- 退保计划 -->
      <RefundPlan v-model="showRefundPlan" :data="current" @done="reload" />
    </ele-card>
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import { pageInsurance, updateInsurancePdf } from '@/api/company';
  import Search from './components/search.vue';
  import Refund from '@/views/insurance/refund/components/create.vue';
  import RefundPlan from '@/views/insurance/refund-plan/components/create.vue';
  import file from '@/components/FileUpload/file.vue';
  /** 表格实例 */
  const tableRef = ref(null);
  const defaultWhere = {
    status: 10
  };
  const current = ref([]);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        width: 70,
        prop: 'id',
        label: 'ID'
      },
      {
        width: 100,
        slot: 'status',
        label: '状态'
      },
      {
        width: 120,
        prop: 'plate_number',
        label: '车牌'
      },
      {
        width: 200,
        prop: 'company',
        label: '车队'
      },
      {
        width: 120,
        prop: 'cate',
        label: '保险类型'
      },
      {
        width: 120,
        prop: 'price',
        label: '金额'
      },
      {
        width: 330,
        prop: 'time',
        label: '时间'
      },
      {
        width: 80,
        slot: 'pdf',
        label: '电子保单',
        align: 'center'
      },
      {
        width: 110,
        slot: 'updatePdf',
        label: '更新保单',
        align: 'center'
      },
      {
        slot: 'action',
        label: '操作',
        fixed: 'right'
      }
    );

    return cols;
  });

  /** 表格数据源 */
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageInsurance({
      ...where,
      ...orders,
      page,
      limit
    });
    return result;
  };
  /** 退保计划 */
  const showRefundPlan = ref(false);
  const openRefundPlan = (row) => {
    current.value = row;
    showRefundPlan.value = true;
  };
  /** 退保 */
  const showRefund = ref(false);
  const openRefund = (row) => {
    current.value = row;
    showRefund.value = true;
  };
  /** 上传pdf完毕 */
  const onUpladDone = (e, row) => {
    console.log('上传成功', e, row);
    const requestData = {
      url: e,
      id: row.id,
      vehicle_id: row.vehicle_id,
      e_time: row.e_time,
      cate: row.cate,
      trade_id: row.trade_id ?? 0,
      action: '更新保单'
    };
    updateInsurancePdf(requestData)
      .then((res) => {
        console.log('更新成功', res);
        reload();
      })
      .catch(() => {});
  };
  /** 搜索 */
  const reload = (where) => {
    tableRef.value?.reload?.({ page: 1, where });
  };
  const openPdf = (url) => {
    window.open(url);
  };
</script>
